package com.yklx.youke.controller;

import com.yklx.youke.entity.Session;
import com.yklx.youke.service.SessionService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/sessions")
@CrossOrigin(origins = "*")
public class SessionController {

    private final SessionService sessionService;

    public SessionController(SessionService sessionService) {
        this.sessionService = sessionService;
    }

    @GetMapping("/available")
    public ResponseEntity<List<Map<String, Object>>> getAvailableTimeSlots(
            @RequestParam UUID teacherId,
            @RequestParam String subject,
            @RequestParam String startDate,
            @RequestParam String endDate) {
        
        List<Map<String, Object>> availableSlots = sessionService.getAvailableTimeSlots(
                teacherId, subject, LocalDate.parse(startDate), LocalDate.parse(endDate));
        
        return ResponseEntity.ok(availableSlots);
    }

    @PutMapping("/{sessionId}/start")
    public ResponseEntity<?> startSession(@PathVariable UUID sessionId) {
        try {
            Map<String, Object> result = sessionService.startSession(sessionId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    @PutMapping("/{sessionId}/complete")
    public ResponseEntity<?> completeSession(@PathVariable UUID sessionId, @RequestBody Map<String, Object> requestData) {
        try {
            String feedback = (String) requestData.get("feedback");
            Integer rating = (Integer) requestData.get("rating");
            
            Map<String, Object> result = sessionService.completeSession(sessionId, feedback, rating);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }
}